//-----------------------------------------------------------------------
// <copyright file="a_ReferSourceDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_ReferSourceDAOBase
    {
        #region Common methods
        public virtual a_ReferSource Createa_ReferSourceFromReader(IDataReader reader)
        {
            a_ReferSource item = new a_ReferSource();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("RefID"))) item.RefID = (int)reader["RefID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RefName"))) item.RefName = (string)reader["RefName"];
                if (!reader.IsDBNull(reader.GetOrdinal("URL"))) item.URL = (string)reader["URL"];
                if (!reader.IsDBNull(reader.GetOrdinal("NumHits"))) item.NumHits = (int)reader["NumHits"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_ReferSource methods
            
        public virtual void Createa_ReferSource(a_ReferSource a_ReferSource)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferSources_ins");
                
                database.AddInParameter(dbCommand, "@RefName", DbType.String, a_ReferSource.RefName);
                database.AddInParameter(dbCommand, "@URL", DbType.AnsiString, a_ReferSource.URL);
                database.AddInParameter(dbCommand, "@NumHits", DbType.Int32, a_ReferSource.NumHits);
                database.AddOutParameter(dbCommand, "@RefID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_ReferSource.RefID = (int)database.GetParameterValue(dbCommand, "@RefID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_ReferSource methods
        
        public virtual void Updatea_ReferSource(a_ReferSource a_ReferSource)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferSources_upd");            
                
                database.AddInParameter(dbCommand, "@RefID", DbType.Int32, a_ReferSource.RefID);
                database.AddInParameter(dbCommand, "@RefName", DbType.String, a_ReferSource.RefName);
                database.AddInParameter(dbCommand, "@URL", DbType.AnsiString, a_ReferSource.URL);
                database.AddInParameter(dbCommand, "@NumHits", DbType.Int32, a_ReferSource.NumHits);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_ReferSource methods
        public virtual void Deletea_ReferSource(int refID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferSources_del");
                
                database.AddInParameter(dbCommand, "@RefID", DbType.Int32, refID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_ReferSource methods
        
        public virtual a_ReferSource Geta_ReferSource(int refID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferSources_sel");
                
                database.AddInParameter(dbCommand, "@RefID", DbType.Int32, refID);
                
                a_ReferSource a_ReferSource =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_ReferSource = Createa_ReferSourceFromReader(reader);
                        reader.Close();
                    }
                }
                return a_ReferSource;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_ReferSourceList methods
        public virtual a_ReferSourceCollection Geta_ReferSourceList(a_ReferSourceColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_ReferSources_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_ReferSourceCollection a_ReferSourceCollection = new a_ReferSourceCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_ReferSource a_ReferSource = Createa_ReferSourceFromReader(reader);
                        a_ReferSourceCollection.Add(a_ReferSource);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_ReferSourceCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_ReferSourceCollection Geta_ReferSourceList(a_ReferSourceColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_ReferSourceList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}